package ui;

import static java.lang.Math.random;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;

import ui.model.TableListModel;
import databaseConnect.AccessConnect;

public class SimulationOriginatorIFrame extends JInternalFrame {
	// Variables
	private static String windowTitle = "neue Simulation erstellen";

	private Vector<String> keys = null;
	private Vector<Vector<String>> rows = null;
	private CheckList_Universal keyValueList = null;

	private BorderLayout layout = new BorderLayout();

	// private Classes
	class scenarioToolbar extends JToolBar {
		// Variables
		private JButton saveScenario = null;
		private String saveScenarioButtonTitle = "Speichern";
		private JButton loadScenario = null;
		private String loadScenarioButtonTitle = "Laden";
		private scenarioToolbar instance = null;

		// Methods
		public scenarioToolbar() {
			super();
			this.add(this.getSaveButton());
			this.add(this.getLoadButton());
		}

		private JButton getSaveButton() {
			if (saveScenario == null) {
				saveScenario = new JButton(saveScenarioButtonTitle);
				saveScenario.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						AccessConnect.executeInsert("SimulationDescription", getKeyValueListContent());
					}

				});
			}
			return saveScenario;
		}

		private JButton getLoadButton() {
			if (loadScenario == null) {
				loadScenario = new JButton(loadScenarioButtonTitle);
				loadScenario.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						SimulationDesktop.getInstance().add(
								new ScenarioOriginatorIFrame());
					}

				});
			}
			return loadScenario;
		}
	}

	public SimulationOriginatorIFrame() {
		super(windowTitle, // title
				true, // resizable
				true, // closeable
				true, // maximizable
				true); // iconifiable
		this.setBounds((int) (random() * 100), (int) (random() * 100),
				100 + (int) (random() * 400), 100 + (int) (random() * 300));
		this.setLayout(layout);
		this.add(new scenarioToolbar(), BorderLayout.NORTH);
		this.add(new JScrollPane(getKeyValueList()), BorderLayout.CENTER);
		this.setVisible(true);
	}

	private Vector<String> getKeys() {
		if (!AccessConnect.is_connected) {
			AccessConnect.connect();
		}
		keys = AccessConnect.getColumnNamesForSelect("SELECT * FROM TestTabelle");
		return keys;
	}

	private Vector<Vector<String>> getRows() {
		if (!AccessConnect.is_connected) {
			AccessConnect.connect();
		}
		rows = AccessConnect.executeSelect(("SELECT * FROM TestTabelle"));
		return rows;
	}

	Color hellblau = new Color(0,255,255);
	Color gruen = new Color(0,255,0);
	Color rot = new Color(255,0,0);

	private CheckList_Universal getKeyValueList() {
		if (keyValueList == null) {
			Vector<Color> tmp = new Vector<Color>();
			for (int i = 0; i < getKeys().size(); i++){
				if (i % 3 == 0){
					tmp.add(gruen);
				} else if (i % 3 == 1){
					tmp.add(rot);
				} else {
					tmp.add(hellblau);	
				}				
			}
			keyValueList = new CheckList_Universal(getKeys(), tmp);
		}
		return keyValueList;
	}
	
	private Vector<Vector<String>> getKeyValueListContent() {
		if (keyValueList == null) {
			return new Vector<Vector<String>>();
		}
		return ((TableListModel)keyValueList.getModel()).getAllValues();
	}
}